const Sequelize = require('sequelize')
const conf = require('../config/mysql')

const sequelize = new Sequelize(conf.database, conf.user, conf.password, {
	host: conf.host,
	dialect: 'mysql',
	pool: {
		max: 5,
        min: 0,
        acquire: 30000,
		idle: 30000
    },
    operatorsAliases: false
})

// 用户表映射对象
const Users = sequelize.define('users', 
	{
		id: {
			type: Sequelize.INTEGER,
			primaryKey: true,
			autoIncrement: true
		},
		openId: Sequelize.STRING,			// 微信小程序标识
		name: Sequelize.STRING,				// 昵称
		resume: Sequelize.STRING,			// 简介
		phone: Sequelize.STRING,			// 电话号码
		authLevel: Sequelize.INTEGER,		// 认证登记
		addressIds: Sequelize.STRING,		// 地址
		orders: Sequelize.INTEGER,			// 接单数
		agree: Sequelize.INTEGER,			// 好评
		balance: Sequelize.FLOAT,			// 余额
		createdAt: Sequelize.STRING,		// 创建时间
		authId: Sequelize.INTEGER,			// 认证标识
	}, 
	{
		timestamps: false
	}
)

// 订单表映射对象
const Orders = sequelize.define('orders',
    {
        id: {
            type: Sequelize.INTEGER,
			primaryKey: true,
			autoIncrement: true
		},
		code: Sequelize.STRING,				// 订单号
		label: Sequelize.STRING,			// 标题
		remarks: Sequelize.STRING,			// 备注
        type: Sequelize.STRING,				// 订单类型 { 0: 快递, 1: 外卖 }
		userId: Sequelize.INTEGER,			// 接单人
		cost: Sequelize.FLOAT,				// 花费
		payMethod: Sequelize.INTEGER,		// 支付方式
		fromAddressId: Sequelize.INTEGER,	// 起始地址
		toAddressId: Sequelize.INTEGER,		// 目的地址
		createdAt: Sequelize.STRING,		// 创建时间
		commitAt: Sequelize.STRING,			// 提交时间
		confirmAt: Sequelize.STRING,		// 确认时间
		completeAt: Sequelize.STRING,		// 完成时间
		status: Sequelize.INTEGER,			// 订单状态 { 0: 已发布, 1: 已接单, 2: 已取货, 3: 完成 }
		deadline: Sequelize.STRING,			// 需求期限
	}, 
	{
		timestamps: false
	}
)

// 地址表映射对象
const Address = sequelize.define('addresses',
    {
        id: {
            type: Sequelize.INTEGER,
			primaryKey: true,
			autoIncrement: true
		},
		name: Sequelize.STRING(20),
		phone: Sequelize.STRING,
		remarks: Sequelize.STRING,
		lat: Sequelize.FLOAT,
		lon: Sequelize.FLOAT,
		orderId: Sequelize.INTEGER,
		userId: Sequelize.INTEGER,
		status: Sequelize.INTEGER,			// 是否默认地址 { 1: 默认 }
    }, 
	{
		timestamps: false
	}
)

// 金额记录表
const AmountReocrds = sequelize.define('amount_records',
	{
		id: {
			type: Sequelize.INTEGER,
			primaryKey: true,
			autoIncrement: true
		},
		userId: Sequelize.INTEGER,			// 用户标识
		description: Sequelize.STRING,		// 描述
		price: Sequelize.FLOAT,				// 金额
		type: Sequelize.INTEGER,			// 类型
		createdAt: Sequelize.STRING,		// 创建时间			
	}, 
	{
		timestamps: false
	}
)

// 用户认证表
const UserAuths = sequelize.define('auths',
	{
		id: {
			type: Sequelize.INTEGER,
			primaryKey: true,
			autoIncrement: true
		},
		school: Sequelize.STRING,			// 学校
		studentId: Sequelize.STRING,		// 学号
		name: Sequelize.STRING,				// 真实姓名
		phone: Sequelize.CHAR(11),			// 联系电话
		college: Sequelize.STRING,			// 院系
		userId: Sequelize.INTEGER,			// 用户标识
		createdAt: Sequelize.STRING,		// 申请认证时间
		status: Sequelize.INTEGER,			// 申请状态 { 0: 申请中, 1: 通过, 2: 未通过 }
	}, 
	{
		timestamps: false
	}
)

// 用户表关联认证表
Users.belongsTo(UserAuths, {
	as: 'auth',
	foreignKey: {
		authId: 'id'
	}
})

// 金额记录表关联用户表
AmountReocrds.belongsTo(Users)

// 订单表关联用户表
Orders.belongsTo(Users)

// 订单表关联地址表
Orders.belongsTo(Address, {
	as: 'toAddress',
	foreignKey: {
		toAddressId: 'orderId'
	}
})
Orders.belongsTo(Address, {
	as: 'fromAddress',
	foreignKey: {
		fromAddressId: 'orderId'
	}
})

module.exports = {
	Users, 
	Orders,
	Address,
	UserAuths
}